﻿package mortal.game.view.skillProgress
{
    import Message.DB.Tables.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import flash.display.*;
    import mortal.game.manager.*;
    import mortal.mvc.core.*;

    public class SkillProgressView extends View
    {
        protected var mv:MovieClip;
        protected var _timer:FrameTimer;
        protected var _repeatTime:int = 0;
        protected var _totalTime:int = 0;
        protected var _callback:Function;
        private static var _instance:SkillProgressView;

        public function SkillProgressView()
        {
            this.layer = LayerManager.uiLayer;
            this.mv = new SkillProgressMovie();
            this.addChild(this.mv);
            this.mv.stop();
            this.mv.y = -10;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.mv.stop();
            return;
        }// end function

        override public function stageResize() : void
        {
            this.x = Global.stage.stageWidth / 2 - 125;
            this.y = Global.stage.stageHeight - 170;
            return;
        }// end function

        public function startByTotalTime(param1:int, param2:Function = null) : void
        {
            Log.error("采集startByTotalTime, totalTime=" + param1);
            this._callback = param2;
            if (!this._timer)
            {
                this._timer = new FrameTimer();
                this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrame);
            }
            this._timer.start();
            this._repeatTime = 0;
            this._totalTime = param1;
            this.show();
            return;
        }// end function

        public function start(param1:TSkill) : void
        {
            Log.error("start(skill:TSkill):void");
            if (!this._timer)
            {
                this._timer = new FrameTimer();
                this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrame);
            }
            this._timer.start();
            this._repeatTime = 0;
            if (param1.useTime > 0)
            {
                this._totalTime = param1.useTime;
            }
            else
            {
                this._totalTime = param1.leadTime;
            }
            this.show();
            return;
        }// end function

        protected function onEnterFrame(param1:FrameTimer) : void
        {
            this._repeatTime = this._repeatTime + param1.interval;
            if (this._repeatTime > this._totalTime)
            {
                this._totalTime = this._totalTime;
                if (this._callback != null)
                {
                    this._callback.apply();
                    this._callback = null;
                }
            }
            var _loc_2:* = Math.ceil(this._repeatTime / this._totalTime * 100);
            this.mv.gotoAndStop(_loc_2);
            return;
        }// end function

        public function updateCount(param1:TSkill, param2:int) : void
        {
            Log.error("读条：updateCount, count=" + param2);
            if (param2 == -1 || param2 == -2 || param1.leadCount == 0)
            {
                if (this.mv != null)
                {
                    this.mv.gotoAndStop(0);
                }
                this.hide();
                if (this._timer != null)
                {
                    this._timer.stop();
                }
            }
            return;
        }// end function

        public static function get instance() : SkillProgressView
        {
            if (!_instance)
            {
                _instance = new SkillProgressView;
                _instance.stageResize();
            }
            return _instance;
        }// end function

        public static function get isInProgress() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
